﻿//
// $HeadURL: http://codeka-memcache.googlecode.com/svn/trunk/Codeka.Memcache.Tests/Helpers/ConfigHelper.cs $
// $Revision: 8 $
// $Author: dean.codeka $     $Date: 2010-02-20 12:45:08 +0000 (Sat, 20 Feb 2010) $
//
// Copyright © 2009-2010 Dean Harding, all rights reserved.
//

using System.Configuration;
using System.IO;
using System.Text;
using Codeka.Memcache.Config;

namespace Codeka.Memcache.Tests.Helpers
{
    /// <summary>
    /// This is just a helper for unit testing configuration files.
    /// </summary>
    static class ConfigHelper
    {
        /// <summary>
        /// Loads a configuration from the given XML string.
        /// </summary>
        /// <returns>The <see cref="Configuration"/> we loaded.</returns>
        /// <remarks>
        /// <para>This is actually not that hard. We have to write the XML out to
        /// a temporary file</para>
        /// </remarks>
        public static Configuration LoadConfiguration(string xml)
        {
            // write the XML out to a file
            var fileName = Path.GetTempFileName();
            using (var outs = new FileStream(fileName, FileMode.Open, FileAccess.Write))
            using (var writer = new StreamWriter(outs, Encoding.UTF8))
            {
                writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                writer.WriteLine();
                writer.Write(xml);
            }

            // register the file to be deleted when the current test finishes
            BaseTestClass.AddFileToBeDeleted(fileName);

            // create an ExeConfigurationFileMap that'll map to our temp file and
            // then load the configuration
            var fileMap = new ExeConfigurationFileMap {
                    ExeConfigFilename = fileName,
                };
            return ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        }

        /// <summary>
        /// Loads the "default" configuration we'll use for most of the tests.
        /// </summary>
        public static IMemcacheConfiguration LoadConfiguration(params int[] ports)
        {
            var xml = @"
                <configuration>
                    <configSections>
                        <section name=""memcache"" type=""Codeka.Memcache.Config.MemcacheClientConfigurationSection, Codeka.Memcache"" />
                    </configSections>
                    <memcache>
                        <servers>";
            foreach(var port in ports)
                xml += "\r\n                            <add address=\"127.0.0.1:" + port + "\" />";
            xml += @"
                        </servers>
                    </memcache>
                </configuration>
            ";

            var config = LoadConfiguration(xml);
            return new DefaultConfiguration((MemcacheClientConfigurationSection)config.GetSection("memcache"));
        }

        /// <summary>
        /// Loads the "default" configuration we'll use for most of the tests.
        /// </summary>
        public static IMemcacheConfiguration LoadDefaultConfiguration()
        {
            return LoadConfiguration(27351);
        }
    }
}
